﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using MathWorks.MATLAB.NET.Arrays;
using configDevice;
using findDevice;
using System.Net;
using JZNJ.RadioMonitorServer.Core;
using System.Threading;
using System.Text.RegularExpressions;


namespace JZNJ.RadioMonitorServer.App
{
    public class DeviceManager
    {
        //private static DeviceManager instance;

        //public static DeviceManager Instance
        //{
        //    get
        //    {
        //        if (instance == null)
        //        {
        //            instance = new DeviceManager();
        //        }
        //        return instance;
        //    }
        //}

        public Dictionary<IPAddress, USRPDevice> DeviceList
        {
            get;
            private set;
        }

        private bool flag = true;

        //public void InitDeviceList()
        //{

        //}

        public event Action<USRPDevice, DeviceInfo> DeviceDataRefresh;

        public Thread WorkThread;

        public void StartGetData()
        {
            if (WorkThread != null)
            {
                WorkThread.Abort();
            }
            WorkThread = new Thread(Work);
            flag = true;
            WorkThread.Start();
        }

        private void Work()
        {
            while (flag)
            {
                foreach (var device in DeviceList)
                {
                    device.Value.RefreshState();
                    if (DeviceDataRefresh != null)
                        DeviceDataRefresh(device.Value, device.Value.Info);
                }
            }
            WorkThread = null;
        }

        public void StopGetData()
        {
            flag = false;
            Thread.Sleep(1000);
            if (WorkThread != null)
            {
                WorkThread.Abort();
            }
            WorkThread = null;
        }

        //声明并初始化变量
        static string[] m_strIPAddress = null;
        static string m_strState = null;
        static DataTable m_tb = new DataTable();

        //查找设备
        public void FindEquipment()
        {
            findDevice.functions f = new findDevice.functions();
            MWArray[] l_Arrary = null;
            l_Arrary = f.findDevice(1);
            m_strIPAddress = new string[] { ((MWCharArray)l_Arrary[0]).ToString() };
        }

        //连接设备
        public void ConnectEquipment()
        {
            InitDataTable();

            configDevice.functions f = new configDevice.functions();
            MWStructArray l_structArrry = null;
            MWArray[] l_strArray = null;

            //正则表达式，用于格式化字符串
            RegexOptions options = RegexOptions.None;
            Regex regex = new Regex(@"[ ]{2,}", options);//用于匹配大于等于的空格

            for (int i = 0; i < m_strIPAddress.Length; i++)
            {
                l_strArray = f.configDevice(1, m_strIPAddress[i], 1000000);
                l_structArrry = (MWStructArray)l_strArray[0];
                m_strState = regex.Replace(Regex.Replace(l_structArrry.ToString().Trim(), "\n", ""), @" ");
                m_strState = Regex.Replace(Regex.Replace(m_strState, ": ", ":")," ",";  ");
                SetDataTable();
            }
        }

        //构造表的格式
        private void InitDataTable()
        {
            m_tb.Columns.Add("ip_address", typeof(string));
            m_tb.Columns.Add("connect_tag", typeof(string));
        }

        //填充DataTable
        public void SetDataTable()
        {
            DataRow l_dr = m_tb.NewRow();
            l_dr["ip_address"] = m_strIPAddress[0];
            l_dr["connect_tag"] = m_strState;
            m_tb.Rows.Add(l_dr);
        }

        //返回DataTable
        public DataTable GetDataTable()
        {
            return m_tb;
        }

        //
    }
}
